#include <cstdio>
#include <cstring>

const int N = 4e7 + 5;
int n, mode, len;
int q[N], f[N];
long long b[N], sum[N];
char str[40];

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("P5665 划分.in", "r", stdin);
  freopen("P5665 划分.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &mode);
  if (!mode) {
    for (int i = 1; i <= n; ++i) {
      scanf("%lld", &sum[i]);
      sum[i] += sum[i - 1];
    }
  } else {
    int m, p, lasp = 0, l, r, mod = (1 << 30) - 1;
    long long x, y, z;
    scanf("%lld%lld%lld%lld%lld%d", &x, &y, &z, &b[1], &b[2], &m);
    for (int i = 3; i <= n; ++i) {
      b[i] = (((((x * b[i - 1]) & mod) + ((y * b[i - 2]) & mod)) & mod) + z) & mod;
    }
    for (int i = 1; i <= m; ++i) {
      scanf("%d%d%d", &p, &l, &r);
      for (int j = lasp + 1; j <= p; ++j) {
        sum[j] = sum[j - 1] + b[j] % (r - l + 1) + l;
      }
      lasp = p;
    }
  }
  int l = 1, r = 1;
  q[1] = 0;
  for (int i = 1; i <= n; ++i) {
    while (l < r && sum[i] >= (sum[q[l + 1]] << 1) - sum[f[q[l + 1]]]) ++l;
    f[i] = q[l];
    while (l <= r && (sum[q[r]] << 1) - sum[f[q[r]]] >= (sum[i] << 1) - sum[f[i]]) --r;
    q[++r] = i;
  }
  __int128_t ans = 0;
  for (int i = n; i; i = f[i]) {
    ans += (__int128_t)(sum[i] - sum[f[i]]) * (sum[i] - sum[f[i]]);
  }
  while (ans) str[++len] = ans % 10 + '0', ans /= 10;
  while (len) putchar(str[len--]);
  return 0;
}